$.fn._serialize = function() {
  var da = this.serializeArray();
  var $elrc = $('input[type=checkbox]', this);
  $.each($elrc, function() {
    if ($("input[name='" + this.name + "']:checked").length == 0) {
      da.push({ name: this.name, value: 0 });
    }
  });
  return jQuery.param(da);
}
function ckradd() {
  if ($("#pname").val() == "") {
    errmsg("请输入昵称后再提交");
    $("#pname").focus();
    return false;
  }
  var val = $("#pcontent").val();
  if (val.length < 5 || val.length > 200) {
    errmsg("评论内容必须在5-200字之间，请修改后再提交！");
    $("#pcontent").focus();
    return false;
  }
  return true
}
function delClass(id) {
  var flag = confirm("你确定要删除吗?")
  if (flag) {
    var data = { "id": id };
    $.post("/class/del", $.param(data), function(result) {
      errmsg(result.message);
      if (result.result == '200') {
        window.location.href = '/class'
      }
    }, 'json');

  }
}
function saveClass(t, id) {
  var update = id != 0;
  var tds = $(t).parents("tr:first").find("td")
  var className = prompt("请输入分类名称:", update ? tds.eq(1).text() : "");
  if (!className) {
    return false;
  }
  if (id == 0 && className.trim() == "") {
    errmsg("分类名称不能空");
    return false;
  }
  var ord = prompt("请输入排序:", update ? tds.eq(2).text() : 0);
  if (!ord) {
    return false;
  }

  var data = { "id": id, "class": className, "ord": ord };
  $.post("/class/save", $.param(data), function(result) {
    errmsg(result.message);
    if (result.result == '200') {
      window.location.href = '/class'
    }
  }, 'json');
  return true
}
function errmsg(s, el) {
  if (!arguments[1]) el = "";
  $(el + '#errmsg').show().text(s).fadeOut(2000);
}
function ckse() {
  var val = $("#key").val();
  if (val.length < 2 || val.length > 10) {
    alert("关键词必须在2-10字之间，请修改后再提交！");
    $("#key").focus();
    return false;
  }
}

function savelog() {
  var data = $("#post").serializeArray();
  var style = $("[name=style]").val();
  log = style == 1 ? $("#log1").val() : editor.$txt.html();
  log = log.replace(/<p>[<br>]*<\/p>/g, '').trim();
  hide = $('#hideEl').prop("checked") ? 1 : 0;
  lock = $('#lockEl').prop("checked") ? 1 : 0;
  data.push({ name: 'pic', value: upic }, { name: 'content', value: log }, { name: 'hide', value: hide }, { name: 'lock', value: lock });
  if (log == "" && pic == "") {
    errmsg("写点什么吧！");
    $("#log").focus();
    return false;
  }
  var title = $("input[name='title']").val().trim();
  if (title == "") {
    errmsg("标题不能为空！");
    $("#title").focus();
    return false;
  }
  $.post("/post", $.param(data), function(result) {
    errmsg(result.message);
    if (result.result == '200') {
      window.location.href = '/topic/get?id=' + result.data;
    }
  }, 'json');

}
function saveset() {
  var data = $("#formset")._serialize();
  $.post("/setting", data, function(data) {
    errmsg(data.data);
    // window.location.href = "/setting";
  }, 'json');
}
function savewid(id) {
  var el = $("#formwid" + id + " input[name='title']")
  var data = $("#formwid" + id).serialize();
  if (el.val() == '') {
    el.focus();
    errmsg("标题不能为空！", "#formwid" + id + " ");
    return false
  };
  $.post("/widget", data, function(data) {
    errmsg(data.message, "#formwid" + id + " ")
    location.reload();
  }, 'json');

}
function delwid(id) {
  if (confirm('确定要删除吗?')) {
    $.post("/widget/del", { id: id }, function(data) { if (data.result == '200') { window.location.reload(); } else { alert(data.message); } }, 'json');
  }
}

function dellog(id, v) {
  if (confirm('确定要删除吗?')) {
    $.post("/topic/del", { id: id }, function(data) {
      if (data.result == '200') {
        if (v == '1') {
          location.href = "/index";
        } else { $("#topic-" + id).fadeOut(); }
      } else {
        alert(data.message);
      }
    }, 'json');
  }
}
function delpl(id, pid) {
  if (confirm('确定要删除吗?')) {
    $.post("/reply/del", { id: id, cid: pid }, function(data) {
      if (data.result == '200') {
        $("#Com-" + id).fadeOut();
      } else {
        alert(data.message);
      }
    }, 'json');
  }
}
function shpl(id) {
  $.post("/reply/audit", { id: id }, function(data) { if (data.result == '200') { $("#sh-" + id).fadeOut(); } else { alert(data.message); } }, 'json');
}
function zdlog(id, flag) {
  var zdobj = $("#zd-" + id);
  var ist = zdobj.attr("ist") == "1" ? 0 : 1;
  $.post("/topic/top", { id: id, ist: ist }, function(data) {
    if (data.result == '200') {
      if (flag == 1) {
        location.reload();
      } else {
        location.href = "/index";
      }
    } else {
      alert(data.message);
    }
  }, 'json');
}
function addpl(id) {
  var ck = ckradd();
  if (ck === false) {
    return ck;
  }
  var pname = $("#pname").val();
  var pcontent = $("#pcontent").val();
  $.post("/reply", { cid: id, pname: pname, pcontent: pcontent }, function(data) {
    if (data.result == '200') {
      location.href = "/topic/get?id=" + id + "&rid=" + data.data;
    } else {
      errmsg(data.message);
    }
  }, 'json');
}
function repl(id) {
  var ore = $('#Ctext-' + id).find('.re span').text();
  var x = 1;
  if (ore == "") { x = 0; }
  var rebox = '<div class="rebox"><textarea required="required" placeholder="请输入回复内容..." name="rlog" rows="3" id="rlog" class="input_narrow relog">' + ore + '</textarea> <button name="re" id="re" class="btn" onclick="plsave(' + id + ',' + x + ')"> 回 复 </button> <button onclick="capl()" class="btn"> 取 消 </button></div>';
  $('.rebox').remove();
  $('#Ctext-' + id).append(rebox);
}
function capl() {
  $('.rebox').remove();
}
function plsave(id, x) {
  var rcontent = $("#rlog").val();
  if (rcontent == '') {
    $("#rlog").focus();
    return false;
  }
  $.post("/reply/reply", {
    rcontent: rcontent,
    id: id
  }, function(data) {
    capl();
    if (data.result == '200') {
      if (x == 1) {
        $('#Ctext-' + id).find('.re span').text(rcontent);
      } else {
        $('#Ctext-' + id).append('<p class="re">&nbsp;&nbsp;<strong style="color:#C00">回复</strong>：<span>' + rcontent + '</span></p>');
      }

    } else {
      alert(data.message);
    }
  }, 'json');
}

function login() {
  var redirect = $("#redirect").val();
  var pwd = $("#pwd").val();
  if (pwd.trim() != '') {
    $.post("/login", { pwd: pwd, redirect: redirect }, function(data) {
      if (data.result == '200') {
        location.href = data.data;
      } else {
        $("#errmsg").text(data.message);
      }
    }, 'json');
  } else {
    $("#pwd").focus();
  }
  return false;
}

function ckpass(id) {
  var pass = $("#password").val();
  if (pass != '') {
    $.post("/topic/pass", { id: id, pass: pass }, function(data) {
      if (data.result == '200') {
        $("#article .text").html(data.data)
      } else {
        alert(data.message);
      }
    }, 'json');
  } else {
    $("#password").focus();
  }
  return false;
}

function DotRoll(elm) {
  $("body,html").animate({ scrollTop: $("a[name='" + elm + "']").offset().top }, 500);
}


$(document).ready(function() {

  $('.textPost').on("click", function(e) {
    window.location.href = $(this).data('url');
  });

  $('#menu_toggle').on("click", function(e) {
    e.preventDefault();
    if ($(".tags:visible").length > 0) {
      $('.key h3').click();
    }
    $('#menu').toggleClass('close');
    $('#nav').slideToggle();
  });

  $('.key h3').on("click", function(e) {
    var w = $(window).width();
    if (w > 650) {
      return;
    }

    if ($("#nav:visible").length > 0) {
      $('#menu_toggle').click();
    }
    $('.key h3').toggleClass('cls');

    var tag = $(".tags");
    if (tag.is(":hidden")) {
      tag.css('display', 'flex');
    } else {
      tag.css('display', 'none');
    }
  });

  $(window).resize(function() {
    var w = $(window).width();
    if (w > 650) {
      $('#nav').show();
    } else {
      $('#menu').removeClass('close');
      $('#nav').hide();
    }
  });

  var url = String(window.location);
  var last = url.charAt(url.length - 1);
  $("#nav li a").each(function(e) {
    if (last == '/') {
      $(this).addClass("on");
      return false;
    }
    else if (url == $(this)[0].href) {
      $(this).addClass("on");
      return false;
    }

  });
  var $backToTopEle = $('<a href="javascript:void(0)" class="Hui-iconfont toTop" title="返回顶部" alt="返回顶部" style="display:none">^</a>').appendTo($("body")).click(function() {
    $("html, body").animate({ scrollTop: 0 }, 120);
  });
  var backToTopFun = function() {
    var st = $(document).scrollTop(),
      winh = $(window).height();
    (st > 0) ? $backToTopEle.show() : $backToTopEle.hide();
    /*IE6下的定位*/
    if (!window.XMLHttpRequest) {
      $backToTopEle.css("top", st + winh - 166);
    }
  };
  $(window).on("scroll", backToTopFun);
  backToTopFun();

  //百度主动推送
  /*
  (function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https'){
     bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else{
    bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
  })();
  */

  var w = $(window).width();
  if (w > 650 && $("#sidebar").length > 0) {
    window.webx.fixedSidebar({
      targetBox: $('#sidebar'), //判断到达底部的元素
      mainBox: $("#main"), //侧边栏旁边的主体元素
      sidebar: $("#sidebar"), //侧边栏主体元素，可与判断到达底部的元素相同
      direction: 'right', //侧边栏浮动方向
      position: 'fixed',//定位方式
      top: 100,
      bottom: 65,
      zIndex: 99,
      sidebarRightMargin: 13,//direction为left,侧边栏距离mainbox的右边距
      sidebarLeftMargin: 20,//direction为right,侧边栏距离mainbox的左边距
    });
  }

})
